package com.example.myapplication;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private static final int SUBACTIVITY1 = 1;
    private static final int SUBACTIVITY2 = 2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
//        startActivityDemo();
//        receiveSubActivity();

        //广播消息
        EditText editText = findViewById(R.id.editText);
        Button subButton = findViewById(R.id.button_sub);
        subButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String msg = editText.getText().toString();
                String action = "com.example.myapplication";
                Intent intent = new Intent(action);
                intent.setFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
                intent.putExtra("msg", msg);
                //广播
                sendBroadcast(intent);
            }
        });
    }
    //接受子组件返回
    private void receiveSubActivity(){
        Button button1 = (Button) findViewById(R.id.startButton1);
        Button button2 = (Button) findViewById(R.id.startButton2);
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this, SubActivity1.class);
                startActivityForResult(intent, SUBACTIVITY1);
            }
        });
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this, SubActivity2.class);
                startActivityForResult(intent, SUBACTIVITY2);
            }
        });
    }

    //接受子Activity返回值
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        TextView textView = (TextView) findViewById(R.id.textView);
        if(resultCode == Activity.RESULT_OK){
            Uri uriData = data.getData();
            if(requestCode == SUBACTIVITY1){
                textView.setText("activity1:" + uriData +"\n" + "address:" + data.getStringExtra("address"));
            }else if(requestCode == SUBACTIVITY2){
                textView.setText("activity2:" + uriData +"\n" + "address:" + data.getStringExtra("address"));
            }
        }
    }

    //启动activity
    private void startActivityDemo(){
        Button button = (Button) findViewById(R.id.startButton1);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //显示启动
//                Intent intent = new Intent(MainActivity.this, DemoActivity.class);
//                startActivity(intent);
                //隐式启动
                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.baidu.com"));
                startActivity(intent);
            }
        });
    }
}